<?php

namespace App\Models;

use Encore\Admin\Traits\AdminBuilder;
use Encore\Admin\Traits\ModelTree;
use Illuminate\Database\Eloquent\SoftDeletes;

class Category extends Base
{
    /**
     * 禁止被批量赋值的字段
     *
     * @var array
     */
    protected $guarded = [];

    // 软删除
    use SoftDeletes, ModelTree, AdminBuilder;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $this->setParentColumn('pid');
        $this->setOrderColumn('sort');
        $this->setTitleColumn('name');
    }

    /**
     * 关联文章表
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function article(){
        return $this->hasMany(Article::class);
    }

    public function storeData($data){

        //根据是否有id判断修改还是添加
        if(isset($data['id'])&&$data['id']){
            $this->where('id',$data['id'])->update($data);
        }else{
            $this->create($data);
        }
    }
}
